using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.DataTier.Compiled;

namespace OR.Lazarus.Library.DataTier.Compiled.Lazarus
{
	/// <summary>
	/// Defines a contextual view of a single entity within the data definition.
	/// </summary>
   public class BusinessViewDetail : EntityBase
   {
		protected System.String mName;
		protected string mContext;
		protected string mDescription;
	
		protected Derived.DataTableCollection mDataTables;
		protected Derived.RelationCollection mRelationships;

		public BusinessViewDetail()
		{
			mDataTables = new Derived.DataTableCollection();
			mDataTables.ParentEntity = this;
			mRelationships = new Derived.RelationCollection();
			mRelationships.ParentEntity = this;
	
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Context
		{
			get
			{
				return ( mContext );
			}
			set
			{
				mContext = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.DataTableCollection DataTables
		{
			get
			{
				return ( mDataTables );
			}
			set
			{
				mDataTables = value;
			}
		}

		public Derived.RelationCollection Relationships
		{
			get
			{
				return ( mRelationships );
			}
			set
			{
				mRelationships = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mContext = DataUtility.GetString( pSource, "context" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lzc:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vDataTables = ( XmlElement )pSource.SelectSingleNode( "lzc:datatables", PersistenceConfig.Namespaces );
			
			if ( vDataTables != null )
			{
				mDataTables.LoadXml( vDataTables );
			}
			XmlElement vRelationships = ( XmlElement )pSource.SelectSingleNode( "lzc:relationships", PersistenceConfig.Namespaces );
			
			if ( vRelationships != null )
			{
				mRelationships.LoadXml( vRelationships );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lzc", "businessview", "http://schemas.icitadel.com/Lazarus/Compiled.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mContext ) )
			{
				vNewElement.SetAttribute( "context", mContext );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lzc", "description", "http://schemas.icitadel.com/Lazarus/Compiled.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mDataTables.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mRelationships.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.BusinessViewDetail v_oReturn = new Derived.BusinessViewDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Context = mContext;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.DataTableDetail vDetail in mDataTables )
			{
				v_oReturn.DataTables.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.RelationDetail vDetail in mRelationships )
			{
				v_oReturn.Relationships.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.BusinessViewDetail CloneTyped()
		{
			return ( ( Derived.BusinessViewDetail )this.Clone() );
		}
   }
}
   